<?php
/**
 * Created by PhpStorm.
 * User: tsang
 * Date: 2018/3/14
 * Time: 10:49
 */

namespace app\admin\controller;


use think\Request;

class Ad extends Common
{

    /**
     * 推广列表
     * @author tsang
     */
    public function index(){
        $list = db('ad')->order('sort desc')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 编辑/添加
     * @author tsang
     */
    public function edit(){
        $id = input('id/d');
        $model = db('ad');
        $brand = db('goods_brand')->select();
        $this->assign('brand',$brand);
        if(request()->isPost()){
            $data = array(
                'type' => input('type/d'),
                'title' => htmlspecialchars(input('title/s')),
                'subtitle' => htmlspecialchars(input('subtitle/s')),
                'is_show' => input('is_show/d'),
                'create_time' => time(),
                'sort' => input('sort/d'),
            );

            $file=Request::instance()->file('image');

            if($file){
                $info = $file->move(ROOT_IMG_PATH .'static/uploads');

                if($info){
                    $data['image']= "/".str_replace('\\',"/",$info->getSaveName());
                }
            }

            switch ($data['type']){
                case 1://商品
                    $goods = db('goods')->where(['id' => input('goods_id/d')])->find();
                    if(!$goods) $this->error('找不到商品');
                    $data['brand_id'] = $goods['brand_id'];
                    $data['content'] = $goods['id'];
                    break;
                case 2://文章
                    $data['content'] = htmlspecialchars(input('content/s'));
                    $data['brand_id'] = input('brand_id/d');
                    break;
                default:
                    $this->error('类型错误');
            }
            if($id > 0){
                if($model->where(['id' => $id])->update($data) !== false){
                    $this->success('更新成功',url('Ad/index'));
                }else{
                    $this->error('更新失败',url('Ad/index'));
                }
            }else{
                if($model->insert($data)){
                    $this->success('添加成功',url('Ad/index'));
                }else{
                    $this->error('添加失败',url('Ad/index'));
                }
            }
        }

        if($id > 0){
            $res = $model->where(['id' => $id])->find();
            if(!$res) $this->error('找不到');
            if($res['type'] == 1){
                $res['goods'] = db('goods')->field('id,name,image')->where(['id' => $res['content']])->find();
            }
            $this->assign('data',$res);
        }

        return $this->fetch();
    }


    /**
     * 删除
     * @author tsang
     */
    public function del(){
        $id = input('id/d');
        $model = db('ad');
        $res = $model->where(['id' => $id])->find();
        if(!$res) $this->error('找不到');
        if($model->where(['id' => $id])->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * 搜索商品
     * @author tsang
     */
    public function select_goods(){
        if(\request()->isAjax()){
            $where=array();
            $keyword = input('keyword');
            if($keyword){
                $where['name']=array("like",'%'.$keyword.'%');
            }
            $goods = db('goods')->field('id,name,image')->where($where)->select();
            echo json_encode($goods);
        }
    }



}